
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class RegisterForm extends Form implements CommandListener {

    private TextField txtUsername;
    private TextField txtPassword;
    private TextField txtRePassword;
    private Command cmdRegister;
    private Command cmdBack;
    private HomeScreen homeScreen;

    public RegisterForm(HomeScreen homeScreen) {
        super("Register");
        this.homeScreen = homeScreen;
        txtUsername = new TextField("Username", null, 30, TextField.ANY);
        txtPassword = new TextField("Password", null, 30, TextField.PASSWORD);
        txtRePassword = new TextField("Re-password", null, 30, TextField.PASSWORD);
        this.append(txtUsername);
        this.append(txtPassword);
        this.append(txtRePassword);
        cmdRegister = new Command("Register", Command.OK, 2);
        cmdBack = new Command("Back",Command.BACK,2);
        this.addCommand(cmdRegister);
        this.addCommand(cmdBack);
        this.setCommandListener(this);
    }

    public void run() {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdRegister) {
            if (!Check()){
                Common.Client.Register(txtUsername.getString(), txtPassword.getString());
            }
        }else if (c==cmdBack){
            homeScreen.DisplayHomeMain();
        }
    }

    private boolean Check() {
        boolean isError = false;
        if (txtUsername.getString().equals("")) {
            this.append("Username exist");
            isError = true;
        }
        if (txtPassword.getString().equals("")) {
            this.append("Password not empty");
            isError = true;
        }
        if (!txtPassword.getString().equals(txtRePassword.getString())) {
            this.append("Password not equal Re-password");
            isError = true;
        }
        return isError;
    }
}
